package com.sinodata.bsm.center.web.action.notify;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.NotifyRuleBean;
import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.bean.TreeData;
import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.cache.NotifyDelayRuleCache;
import com.sinodata.bsm.center.cache.NotifyEventTypeRuleCache;
import com.sinodata.bsm.center.cache.NotifyExcludeResRuleCache;
import com.sinodata.bsm.center.cache.NotifyPauseRuleCache;
import com.sinodata.bsm.center.cache.NotifyReceiveRuleCache;
import com.sinodata.bsm.center.cache.NotifyRuleCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.cache.ResTypeCache;
import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UserFunctionValidate;
import com.sinodata.bsm.center.service.notify.NotifyRuleService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.constants.NotifyConstants;
import com.sinodata.bsm.common.utils.CrontabUtil;
import com.sinodata.bsm.common.vo.EventType;
import com.sinodata.bsm.common.vo.NotifyEventTypeRule;
import com.sinodata.bsm.common.vo.NotifyExcludeResRule;
import com.sinodata.bsm.common.vo.NotifyRule;
import com.sinodata.bsm.common.vo.ResType;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-3 PM 2:58:41      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class NotifyRuleAction extends BaseAction<NotifyRule> {

    /**
     * 
     */
    private static final long serialVersionUID = -5851656779848642246L;

    @Autowired
    private NotifyRuleService notifyRuleService;

    @Autowired
    private NotifyRuleCache notifyRuleCache;
    @Autowired
    private ResTypeCache resTypeCache;
    @Autowired
    private EventTypeCache eventTypeCache;
    @Autowired
    private NotifyReceiveRuleCache notifyReceiveRuleCache;

    @Autowired
    private NotifyPauseRuleCache notifyPauseRuleCache;

    @Autowired
    private NotifyExcludeResRuleCache notifyExcludeResRuleCache;

    @Autowired
    private NotifyEventTypeRuleCache notifyEventTypeRuleCache;

    @Autowired
    private NotifyDelayRuleCache notifyDelayRuleCache;
    @Autowired
    private ResCache resCache;
    private Long id;
    private NotifyRule entity;
    private Long resId;

    /**
     * 进入事件通知表格页面
     * 
     * @return
     */
    public String common() {
        Struts2Utils.getRequest().setAttribute("resId", request.getParameter("id"));
        return "common";
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ModelDriven#getModel()
     */
    @Override
    public NotifyRule getModel() {
        return entity;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#list()
     */
    @Override
    public String list() throws Exception {
        List<NotifyRuleBean> list = notifyRuleService.findAllByRes(resId);
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        boolean sysManager = UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER);
        for (NotifyRuleBean notifyRuleBean : list) {
            JSONObject obj = new JSONObject();
            int isExclude = isExclude(resId, notifyRuleBean);
            StringBuilder sb = new StringBuilder();
            if (sysManager) {
                sb.append("&nbsp;&nbsp;&nbsp;");
                if (isExclude == NotifyConstants.RULE_EXCLUDE) {
                    sb.append("<a href='javascript:include(");
                    sb.append(notifyRuleBean.notifyRule().getId());
                    sb.append(",");
                    sb.append(resId);
                    sb.append(");'><img src='../style/default/bsm/images/unblock.gif' title='解除屏蔽'></a>");
                    sb.append("&nbsp;");
                }
                if (isExclude == NotifyConstants.RULE_INCLUDE) {
                    sb.append("<a href='javascript:exclude(");
                    sb.append(notifyRuleBean.notifyRule().getId());
                    sb.append(",");
                    sb.append(resId);
                    sb.append(");'><img src='../style/default/bsm/images/shield.gif' title='屏蔽'></a>");
                    sb.append("&nbsp;");
                }
                if (isExclude == NotifyConstants.RULE_SELF) {
                    // 禁用和启用
                    if (notifyRuleBean.notifyRule().getStatus().equals(NotifyConstants.RULE_STATUS_DISABLE)) {
                        sb.append("<a href='javascript:enable(");
                        sb.append(notifyRuleBean.notifyRule().getId());
                        sb.append(");'><img src='../style/default/bsm/images/start.gif' title='启用'></a>");
                        sb.append("&nbsp;");
                    }
                    if (notifyRuleBean.notifyRule().getStatus().equals(NotifyConstants.RULE_STATUS_ENABLE)) {
                        sb.append("<a href='javascript:disable(");
                        sb.append(notifyRuleBean.notifyRule().getId());
                        sb.append(");'><img src='../style/default/bsm/images/stop.gif' title='禁用'></a>");
                        sb.append("&nbsp;");
                    }
                    sb.append("<a href='javascript:update(");
                    sb.append(notifyRuleBean.notifyRule().getId());
                    sb.append(");'><img src='../style/default/bsm/images/edit.gif' title='修改'></a>");
                    sb.append("&nbsp;");
                    sb.append("<a href='javascript:del(");
                    sb.append(notifyRuleBean.notifyRule().getId());
                    sb.append(");'><img src='../style/default/bsm/images/delete.gif' title='删除'></a>");
                }
            }
            sb.append("</div>");
            //控制屏蔽、禁用 按钮
            obj.element("op", sb.toString());
            obj.element("id", notifyRuleBean.notifyRule().getId());
            obj.element("resName", ResBean.get(notifyRuleBean.notifyRule().getResId()).res().getName());
            obj.element("name", "<a href='javascript:view(" + notifyRuleBean.notifyRule().getId() + ");'>" + notifyRuleBean.notifyRule().getName() + "</a>");
            //obj.element("type", NotifyConstants.getModeDesc(notifyRuleBean.notifyRule().getType()));
            obj.element("eventLevels", transEventLevel(notifyRuleBean.notifyRule().getEventLevels()));
            obj.element("status", NotifyConstants.getRuleStatusDesc(notifyRuleBean.notifyRule().getStatus()));
            obj.element("isExclude", isExclude(resId, notifyRuleBean));//是否继承
            //   obj.element("desc", notifyRule.getStatus());//描述
            array.add(obj);
        }
        json.element("aaData", array);
        renderText(json.toString());
        return null;
    }

    /**
     * 转换事件级别
     * 
     * @param levels
     * @return
     */
    private String transEventLevel(String levels) {
        StringBuilder sb = new StringBuilder("");
        if (levels != null && !"".equals(levels)) {
            String[] temp = levels.split("[,]");
            for (int i = 0; i < temp.length; i++) {
                if (i != 0) {
                    sb.append(",");
                }
                sb.append(EventConstants.getLevelDesc(Integer.valueOf(temp[i])));

            }
        }
        return sb.toString();
    }

    /**
     * 判断是排除的通知规则
     * 1：排除
     * 0：不排除
     * 2：自身的规则
     * 
     * @param resId
     * @param notifyRuleBean
     * @return
     */
    private int isExclude(Long resId, NotifyRuleBean notifyRuleBean) {
        if (resId.equals(notifyRuleBean.notifyRule().getResId())) {
            return NotifyConstants.RULE_SELF;
        }
        List<NotifyExcludeResRule> excludeResRules = notifyRuleBean.getNotifyExcludeResRules();
        if (excludeResRules != null) {
            for (NotifyExcludeResRule notifyExcludeResRule : excludeResRules) {
                if (resId.equals(notifyExcludeResRule.getResId())) {
                    return NotifyConstants.RULE_EXCLUDE;
                }
            }
        }
        return NotifyConstants.RULE_INCLUDE;
    }

    //启用、禁用、屏蔽、解除屏蔽
    /**
     * 禁用事件通知规则
     * 
     * @return
     */
    public String disable() {
        if (notifyRuleCache.get(id) != null) {
            notifyRuleService.disable(id);
            NotifyRule notifyRule = notifyRuleCache.get(id);
            notifyRule.setStatus(NotifyConstants.RULE_STATUS_DISABLE);
            renderText("true");
        } else {
            renderText("false");
        }
        return null;
    }

    /**
     * 启用事件通知规则
     * 
     * @return
     */
    public String enable() {
        if (notifyRuleCache.get(id) != null) {
            notifyRuleService.enable(id);
            NotifyRule notifyRule = notifyRuleCache.get(id);
            notifyRule.setStatus(NotifyConstants.RULE_STATUS_ENABLE);
            renderText("true");
        } else {
            renderText("false");
        }
        return null;
    }

    /**
     * 排除事件通知规则（不继承父资源的事件通知规则）
     * 
     * @return
     */
    public String exclude() {
        if (notifyRuleCache.get(id) != null) {
            NotifyExcludeResRule notifyExcludeResRule = notifyRuleService.exclude(id, resId);
            //缓存数据操作
            notifyExcludeResRuleCache.put(notifyExcludeResRule);
            renderText("true");
        } else {
            renderText("false");
        }
        return null;
    }

    /**
     * 继承父资源的事件通知规则
     * 
     * @return
     */
    public String include() {
        if (notifyRuleCache.get(id) != null) {
            notifyRuleService.include(id, resId);
            //缓存数据操作
            NotifyRuleBean notifyRuleBean = NotifyRuleBean.get(id);
            List<NotifyExcludeResRule> list = notifyRuleBean.getNotifyExcludeResRules();
            for (int i = list.size() - 1; i >= 0; i--) {
                if (list.get(i).getResId().equals(resId)) {
                    list.remove(i);
                }
            }
            renderText("true");
        } else {
            renderText("false");
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#input()
     */
    @Override
    public String input() throws Exception {
        Struts2Utils.getRequest().setAttribute("resId", request.getParameter("resId"));
        Struts2Utils.getRequest().setAttribute("resName", resCache.get(Long.valueOf(request.getParameter("resId"))).getName());
        NotifyRule notifyRule = notifyRuleCache.get(Long.valueOf(request.getParameter("id")));
        List<NotifyEventTypeRule> list = notifyEventTypeRuleCache.getByNotifyRule(notifyRule.getId());
        StringBuffer eventType = new StringBuffer("");
        StringBuffer eventTypeName = new StringBuffer("");
        if (list != null) {
            for (NotifyEventTypeRule notifyEventTypeRule : list) {
                eventType.append(notifyEventTypeRule.getEventTypeId()).append(",");
                eventTypeName.append(eventTypeCache.get(notifyEventTypeRule.getEventTypeId()).getName()).append(",");
            }
        }
        if (eventType.length() != 0) {
            eventType.delete(eventType.lastIndexOf(","), eventType.length());
            eventTypeName.delete(eventTypeName.lastIndexOf(","), eventTypeName.length());
        }
        Struts2Utils.getRequest().setAttribute("eventTypeName", eventTypeName.toString());
        Struts2Utils.getRequest().setAttribute("eventType", eventType.toString());
        Struts2Utils.getRequest().setAttribute("radio", eventType.length() == 0 ? "all" : "other");
        Struts2Utils.getRequest().setAttribute("notifyRule", notifyRule);
        return "update";
    }

    public String checkDeleted() {
        JSONObject ob = new JSONObject();
        if (notifyRuleCache.get(Long.valueOf(request.getParameter("id"))) == null) {
            ob.element("flag", false);
        } else {
            ob.element("flag", true);
        }
        Struts2Utils.renderJson(ob.toString());
        return null;
    }

    public String view() {
        NotifyRule notifyRule = notifyRuleCache.get(Long.valueOf(request.getParameter("id")));
        Struts2Utils.getRequest().setAttribute("resName", resCache.get(notifyRule.getResId()).getName());
        Struts2Utils.getRequest().setAttribute("resId", notifyRule.getResId());
        List<NotifyEventTypeRule> list = notifyEventTypeRuleCache.getByNotifyRule(notifyRule.getId());
        StringBuffer eventType = new StringBuffer("");
        if (list != null) {
            for (NotifyEventTypeRule notifyEventTypeRule : list) {
                eventType.append(notifyEventTypeRule.getEventTypeId()).append(",");
            }
        }
        if (eventType.length() != 0) {
            eventType.delete(eventType.lastIndexOf(","), eventType.length());
        }
        Struts2Utils.getRequest().setAttribute("eventType", eventType.toString());
        Struts2Utils.getRequest().setAttribute("radio", eventType.length() == 0 ? "all" : "other");
        Struts2Utils.getRequest().setAttribute("notifyRule", notifyRule);

        List<NotifyEventTypeRule> listNotifyEventTypeRule = notifyEventTypeRuleCache.getByNotifyRule(notifyRule.getId());
        List<String> eventTypeName = new ArrayList<String>();
        if (listNotifyEventTypeRule != null && listNotifyEventTypeRule.size() != 0) {
            int num = 0;
            for (NotifyEventTypeRule notifyEventTypeRule : listNotifyEventTypeRule) {
                if (num >= 10) {
                    eventTypeName.add("....");
                    break;
                }
                eventTypeName.add(eventTypeCache.get(notifyEventTypeRule.getEventTypeId()).getName());
                num++;
            }
        } else {
            eventTypeName.add("全部");
        }
        Struts2Utils.getRequest().setAttribute("duration", CrontabUtil.parseSimple((notifyRule.getDuration()).intValue()));
        Struts2Utils.getRequest().setAttribute("eventTypeName", eventTypeName);
        return "view";
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#save()
     */
    @Override
    public String save() throws Exception {
        entity.setStatus(NotifyConstants.RULE_STATUS_ENABLE);
        entity.setEventLevels(request.getParameter("eventLevel"));
        if (entity.getId() != null) {//修改
            if (notifyRuleCache.get(entity.getId()) != null) {
                notifyRuleService.update(entity);
                //自定义
                if (!request.getParameter("eventTypeId").equals("")) {
                    List<NotifyEventTypeRule> listOld = notifyEventTypeRuleCache.getByNotifyRule(entity.getId());
                    if (listOld != null && listOld.size() != 0) {
                        notifyRuleService.delEventType(listOld);
                        List<NotifyEventTypeRule> listNew = new ArrayList<NotifyEventTypeRule>();
                        for (NotifyEventTypeRule n : listOld) {
                            NotifyEventTypeRule notifyEventTypeRule1 = new NotifyEventTypeRule();
                            notifyEventTypeRule1.setId(n.getId());
                            notifyEventTypeRule1.setNotifyRuleId(n.getNotifyRuleId());
                            notifyEventTypeRule1.setEventTypeId(n.getEventTypeId());
                            listNew.add(notifyEventTypeRule1);
                        }
                        for (NotifyEventTypeRule notifyEventTypeRule : listNew) {
                            notifyEventTypeRuleCache.remove(notifyEventTypeRule.getId());
                        }
                    }
                    String[] array = request.getParameter("eventTypeId").split(",");
                    List<NotifyEventTypeRule> list = new ArrayList<NotifyEventTypeRule>();
                    for (String eventTypeId : array) {
                        NotifyEventTypeRule notifyEventTypeRule = new NotifyEventTypeRule();
                        notifyEventTypeRule.setNotifyRuleId(entity.getId());
                        notifyEventTypeRule.setEventTypeId(Long.valueOf(eventTypeId));
                        list.add(notifyEventTypeRule);
                    }
                    notifyRuleService.addEventType(list);
                    for (NotifyEventTypeRule notifyEventTypeRule : list) {
                        notifyEventTypeRuleCache.put(notifyEventTypeRule);
                    }
                } else {
                    List<NotifyEventTypeRule> listOld = notifyEventTypeRuleCache.getByNotifyRule(entity.getId());
                    if (listOld != null && listOld.size() != 0) {
                        notifyRuleService.delEventType(listOld);
                        List<NotifyEventTypeRule> listNew = new ArrayList<NotifyEventTypeRule>();
                        for (NotifyEventTypeRule n : listOld) {
                            NotifyEventTypeRule notifyEventTypeRule1 = new NotifyEventTypeRule();
                            notifyEventTypeRule1.setId(n.getId());
                            notifyEventTypeRule1.setNotifyRuleId(n.getNotifyRuleId());
                            notifyEventTypeRule1.setEventTypeId(n.getEventTypeId());
                            listNew.add(notifyEventTypeRule1);
                        }
                        for (NotifyEventTypeRule notifyEventTypeRule : listNew) {
                            notifyEventTypeRuleCache.remove(notifyEventTypeRule.getId());
                        }
                    }
                }
                notifyRuleCache.put(entity);
                JSONObject ob = new JSONObject();
                ob.element("flag", true);
                ob.element("notifyRuleId", entity.getId());
                Struts2Utils.renderJson(ob.toString());
            } else {
                JSONObject ob = new JSONObject();
                ob.element("flag", false);
                Struts2Utils.renderJson(ob.toString());
            }
        } else {//新增
            notifyRuleService.add(entity);

            if (!request.getParameter("eventTypeId").equals("")) {
                String[] array = request.getParameter("eventTypeId").split(",");
                List<NotifyEventTypeRule> list = new ArrayList<NotifyEventTypeRule>();
                for (String eventTypeId : array) {
                    NotifyEventTypeRule notifyEventTypeRule = new NotifyEventTypeRule();
                    notifyEventTypeRule.setNotifyRuleId(entity.getId());
                    notifyEventTypeRule.setEventTypeId(Long.valueOf(eventTypeId));
                    list.add(notifyEventTypeRule);
                }
                notifyRuleService.addEventType(list);
                for (NotifyEventTypeRule notifyEventTypeRule : list) {
                    notifyEventTypeRuleCache.put(notifyEventTypeRule);
                }
            }

            notifyRuleCache.put(entity);
            JSONObject ob = new JSONObject();
            ob.element("notifyRuleId", entity.getId());
            ob.element("flag", true);
            Struts2Utils.renderJson(ob.toString());
        }
        return null;
    }

    public String eventType() {
        Struts2Utils.getRequest().setAttribute("notifyRuleId", request.getParameter("notifyRuleId"));
        Struts2Utils.getRequest().setAttribute("eventType", request.getParameter("eventType"));
        if (request.getParameter("eventType") != null && !request.getParameter("eventType").equals("")) {
            String[] eventTypeIds = request.getParameter("eventType").split(",");
            StringBuffer eventTypeNames = new StringBuffer();
            for (String eventTypeId : eventTypeIds) {
                eventTypeNames.append(eventTypeCache.get(Long.valueOf(eventTypeId)).getName()).append(",");
            }
            if (eventTypeNames.length() != 0) {
                eventTypeNames.delete(eventTypeNames.lastIndexOf(","), eventTypeNames.length());
            }
            Struts2Utils.getRequest().setAttribute("eventTypeName", eventTypeNames);
        }
        return "eventtype";
    }

    public String saveEventType() {
        Struts2Utils.renderJson(request.getParameter("eventTypeId"));
        return null;
    }

    public String checkName() throws Exception {
        String resId = request.getParameter("resId");
        String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
        name = name.replace(" ", "----");
        name = java.net.URLDecoder.decode(name, "UTF-8");
        name = name.replace("----", "+");
        String oldName = "";
        if (request.getParameter("oldName") != null && !request.getParameter("oldName").equals("")) {
            oldName = new String(request.getParameter("oldName").getBytes("ISO-8859-1"), "UTF-8");
            oldName = oldName.replace(" ", "----");
            oldName = java.net.URLDecoder.decode(oldName, "UTF-8");
            oldName = oldName.replace("----", "+");
        }
        if (!name.equals(oldName)) {
            if (notifyRuleService.checkName(Long.valueOf(resId), name)) {
                renderText("true");
            } else {
                renderText("false");
            }
        } else {
            renderText("true");
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#delete()
     */
    @Override
    public String delete() throws Exception {
        if (notifyRuleCache.get(id) != null) {
            notifyRuleService.delete(id);
            //清除缓存数据
            notifyReceiveRuleCache.removeByNotifyRule(id);
            notifyPauseRuleCache.removeByNotifyRule(id);
            notifyExcludeResRuleCache.removeByNotifyRule(id);
            notifyEventTypeRuleCache.removeByNotifyRule(id);
            notifyDelayRuleCache.removeByNotifyRule(id);
            notifyRuleCache.remove(id);
            renderText("true");
        } else {
            renderText("false");
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#prepareModel()
     */
    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = notifyRuleCache.get(id);
            if (entity == null) {
                entity = new NotifyRule();
            }
        } else {
            entity = new NotifyRule();
        }
    }

    /**
     * 跳转至新增主页面
     * 
     * @return
     */
    public String add() {
        Struts2Utils.getRequest().setAttribute("resId", resId);
        Struts2Utils.getRequest().setAttribute("resName", resCache.get(resId) == null ? "请选择资源" : resCache.get(resId).getName());
        return "add";
    }

    public String eventTypeViewTree() {
        String eventType = request.getParameter("eventType");
        String[] array = null;
        if (!eventType.equals("")) {
            array = eventType.split(",");
            Arrays.sort(array);
        }
        List<TreeData> list = getEventTypeTreeData(array);
        JSONArray jsonArray = JSONArray.fromObject(list);
        Struts2Utils.renderText(jsonArray.toString());
        return null;
    }

    public List<TreeData> getEventTypeTreeData(String[] scope) {
        List<TreeData> listResult = new ArrayList<TreeData>();
        //自定义事件类别
        TreeData treeDataCustom = new TreeData();
        treeDataCustom.setId("id");
        treeDataCustom.setName("自定义事件类别");
        treeDataCustom.setNocheck(true);
        treeDataCustom.setIconSkin("type1");
        List<TreeData> childrenCustom = new ArrayList<TreeData>();
        for (EventType eventType : eventTypeCache.getAllCustom()) {
            TreeData treeDataCustom1 = new TreeData();
            treeDataCustom1.setId(String.valueOf(eventType.getId()));
            if (scope != null && Arrays.binarySearch(scope, String.valueOf(eventType.getId())) > -1) {
                treeDataCustom1.setChecked(true);
            }
            treeDataCustom1.setIconSkin("eventtype");
            treeDataCustom1.setName(eventType.getName());
            childrenCustom.add(treeDataCustom1);
        }
        treeDataCustom.setChildren(childrenCustom);
        listResult.add(treeDataCustom);

        List<ResType> listAll = resTypeCache.getAll();
        Collections.sort(listAll);
        List<ResType> list = new ArrayList<ResType>();
        for (ResType resType : listAll) {
            if (resType.getParentId() == null) {
                TreeData treeData = new TreeData();
                treeData.setId(String.valueOf(resType.getId()));
                treeData.setName(resType.getName());
                treeData.setState("open");
                treeData.setIconSkin("type0");
                treeData.setNocheck(true);
                List<EventType> listEventType = eventTypeCache.findByResTypeId(resType.getId());
                if (listEventType.size() != 0) {
                    List<TreeData> children = new ArrayList<TreeData>();
                    TreeData treeData1 = new TreeData();
                    treeData1.setId("id");
                    treeData1.setName("事件类别");
                    treeData1.setIconSkin("type1");
                    treeData1.setNocheck(true);
                    List<TreeData> eventTypeList = new ArrayList<TreeData>();
                    for (EventType eventType : listEventType) {
                        TreeData treeData2 = new TreeData();
                        treeData2.setId(String.valueOf(eventType.getId()));
                        treeData2.setName(eventType.getName());
                        if (scope != null && Arrays.binarySearch(scope, String.valueOf(eventType.getId())) > -1) {
                            treeData2.setChecked(true);
                        }
                        treeData2.setIconSkin("eventtype");
                        eventTypeList.add(treeData2);
                    }
                    treeData1.setChildren(eventTypeList);
                    children.add(treeData1);
                    treeData.setChildren(children);
                }
                listResult.add(treeData);
            } else {
                list.add(resType);
            }
        }
        //循环遍历其下子节点
        getEventTypeTreeDataCycle(list, listResult, scope);
        return listResult;
    }

    private void getEventTypeTreeDataCycle(List<ResType> listScope, List<TreeData> listResult, String[] scope) {
        for (TreeData treeData : listResult) {
            List<TreeData> children = new ArrayList<TreeData>();
            List<ResType> list = new ArrayList<ResType>();
            for (ResType resType : listScope) {
                if (String.valueOf(resType.getParentId()).equals(treeData.getId())) {
                    TreeData treeData1 = new TreeData();
                    treeData1.setId(String.valueOf(resType.getId()));
                    treeData1.setName(resType.getName());
                    treeData1.setIconSkin("type0");
                    treeData1.setNocheck(true);
                    //treeData.setChkDisabled(true);
                    List<EventType> listEventType = eventTypeCache.findByResTypeId(resType.getId());
                    if (listEventType.size() != 0) {
                        List<TreeData> children1 = new ArrayList<TreeData>();
                        TreeData treeData3 = new TreeData();
                        treeData3.setId("id");
                        treeData3.setIconSkin("type1");
                        treeData3.setName("事件类别");
                        treeData3.setNocheck(true);
                        List<TreeData> eventTypeList = new ArrayList<TreeData>();
                        for (EventType eventType : listEventType) {
                            TreeData treeData2 = new TreeData();
                            treeData2.setId(String.valueOf(eventType.getId()));
                            treeData2.setName(eventType.getName());
                            if (scope != null && Arrays.binarySearch(scope, String.valueOf(eventType.getId())) > -1) {
                                treeData2.setChecked(true);
                            }
                            treeData2.setIconSkin("eventtype");
                            eventTypeList.add(treeData2);
                        }
                        treeData3.setChildren(eventTypeList);
                        children1.add(treeData3);
                        treeData1.setChildren(children1);
                    }
                    children.add(treeData1);
                } else {
                    list.add(resType);
                }
            }
            if (children.size() != 0) {
                getEventTypeTreeDataCycle(list, children, scope);
                List<TreeData> listChildren = treeData.getChildren();
                if (listChildren != null) {
                    listChildren.addAll(children);
                } else {
                    listChildren = children;
                }
                treeData.setChildren(listChildren);
            }
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getResId() {
        return resId;
    }

    public void setResId(Long resId) {
        this.resId = resId;
    }

}